<?php

class CWei
{
	const TOKEN = '###token';
	const NAME = '###微信号';
	const MAIN_MEUN = 'm';
	const BACK = 'b';

	public static function get_post()
	{
		// $post = simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
		$post = simplexml_load_string(file_get_contents('php://input', 'r'),  'SimpleXMLElement', LIBXML_NOCDATA);

		if (is_object($post))
		{
			$arr = array();
			foreach ($post as $name => $val)
			{
				$arr[$name] = trim($val);
				// $arr[strtolower($name)] = $arr[$name];
			}
			
			return $arr;
		}
		else
		{
			self::_exit();
		}
	}
	
	public static function _get($name)
	{
		if (isset($_GET[$name]))
		{
			return trim($_GET[$name]);
		}
		else
		{
			return '';
		}
	}
	 
	public static function valid()
	{
		$tmpArr = array(
			self::TOKEN,
			self::_get('timestamp'),
			self::_get('nonce'),
			);
			
		sort($tmpArr, SORT_STRING);

		if (sha1(implode($tmpArr)) == self::_get('signature'))
		{
			if (isset($_GET['echostr']))
			{
				exit($_GET['echostr']);
			};
		}
		else
		{
			self::_exit();
		}
	}
	
	private static function _exit($err = '')
	{
		exit($err);
	}
}
